<!--
 * @Author: 89-83-77
 * @Description: 请填写当前文件简介
 * @Date: 2025-03-27 23:54:42
 * @LastEditTime: 2025-03-27 23:54:52
 * @FilePath: \event-analysis-systemd:\Desktop\client\src\views\system\Role.vue
-->
<template>
    <div class="role-management">
        <el-card>
            <template #header>
                <div class="toolbar">
                    <el-button type="primary" @click="handleCreate">新增角色</el-button>
                </div>
            </template>

            <el-table :data="roleList">
                <el-table-column prop="name" label="角色名称" />
                <el-table-column label="权限菜单">
                    <template #default="{ row }">
                        <el-tag v-for="menu in row.permissions" :key="menu" class="mx-1">
                            {{ menu }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template #default="scope">
                        <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.row.name)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 角色编辑对话框 -->
            <el-dialog v-model="roleDialogVisible" :title="dialogTitle">
                <el-form :model="currentRole" label-width="100px">
                    <el-form-item label="角色名称" required>
                        <el-input v-model="currentRole.name" />
                    </el-form-item>
                    <el-form-item label="权限分配">
                        <el-transfer v-model="currentRole.permissions" :data="menuOptions" :titles="['可用菜单', '已选菜单']" />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <el-button @click="roleDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitRole">确认</el-button>
                </template>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoleStore } from '@/stores/role'
import { useMenuStore } from '@/stores/menu'

const roleStore = useRoleStore()
const menuStore = useMenuStore()

// 获取所有菜单路径作为权限选项
const menuOptions = computed(() => {
    return menuStore.menus.map(menu => ({
        key: menu.path,
        label: menu.meta.title
    }))
})

// 对话框状态
const roleDialogVisible = ref(false)
const dialogTitle = ref('新增角色')
const currentRole = ref({
    name: '',
    permissions: []
})

// 获取角色列表
const roleList = computed(() => roleStore.roles)

const handleCreate = () => {
    currentRole.value = {
        name: '',
        permissions: []
    }
    dialogTitle.value = '新增角色'
    roleDialogVisible.value = true
}

const handleEdit = (role) => {
    currentRole.value = JSON.parse(JSON.stringify(role))
    dialogTitle.value = '编辑角色'
    roleDialogVisible.value = true
}

const submitRole = async () => {
    await roleStore.updateRole(currentRole.value)
    roleDialogVisible.value = false
}

const handleDelete = async (roleName) => {
    await roleStore.deleteRole(roleName)
}
</script>